<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;


class TypeController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
		 //数据库查询
         $list = \DB::select("select * from type order by concat(path,id) asc");
		 //var_dump($list);die;
		 //处理信息
		 foreach($list as $v){
			 $m = substr_count($v->path,',');
			 //获取path中的逗号
			 //生成缩进
			  $v->name = str_repeat(" &nbsp; ",($m-1)*8)."|--".$v->name;
		 }
		   return view("admin.type.index",['list'=>$list]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view("admin.type.create");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {   
	    //接受根分类的值
	    $name = $request->name;
        //数据库查询分类是否存在
		$list=
		\DB::table('type')->where('name',"{$name}")->first(); 
		//判断添加的分类是否存在
		if(!empty($list)){
			echo '<script>alert("根类已经存在");</script>';
			return view('admin.type.create');
		}else{
			//准备要插入表的值
			$pid = 0;
			$path = '0,';
			//执行添加
			$result = \DB::table('type')->insertGetId(['name'=>"{$name}",'pid'=>"{$pid}",'path'=>"{$path}"]);
			//判断
			if($result>0){
				echo '<script>alert("添加成功");</script>';
				 //数据库查询
                 $list = \DB::select("select * from type order by concat(path,id) asc");
		        //处理信息
		        foreach($list as $v){
				 //获取path中的逗号
			     $m = substr_count($v->path,',');
			     //生成缩进
			     $v->name = str_repeat(" &nbsp; ",($m)*8)."|--".$v->name;
		          }
		        return view("admin.type.index",['list'=>$list]);
				
			}else{
				echo '<script>alert("添加失败");</script>';
				return view('admin.type.create');
			}
		}
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
		//数据库查询
		$result = \DB::table('type')->where('id',"{$id}")->first();
		//传输数据
        return view("admin.type.update",['list'=>$result]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //接受传输过来的数据
		$newname = $request->update;
	    //执行修改
		$result=\DB::table('type')->where('id',$id)->update(['name' =>"{$newname}"]);
		//加判断
		if($result>0){
			    echo "<script>alert('修改成功！');</script>";
			    //数据库查询
                 $list = \DB::select("select * from type order by concat(path,id) asc");
		        //处理信息
		        foreach($list as $v){
				 //获取path中的逗号
			     $m = substr_count($v->path,',');
			     //生成缩进
			     $v->name = str_repeat(" &nbsp; ",($m)*8)."|--".$v->name;
		          }
		        return view("admin.type.index",['list'=>$list]);
		}else{
			 echo "<script>alert('修改失败！');</script>";
			 //数据库查询
		     $result = \DB::table('type')->where('id',"{$id}")->first();
		    //传输数据
             return view("admin.type.update",['list'=>$result]);
		}
           
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
		//查询pid=id是否有值，如果有不可删除
		$list=\DB::select("select pid from type where pid={$id}");
		//判断是否有值
		if(!empty($list)){
			echo '<script>alert("添加失败");</script>';
			return redirect('admin/type');
		}else{
            $list2=\DB::table("type")->delete($id);
		       if(!empty($lsit2)){
				     echo "<script>alert('删除成功！');</script>";
		             return redirect('admin/type');
			   }else{
				    echo "<script>alert('删除失败！');</script>";
		             return redirect('admin/type');
			   }
		}
    }
}
